<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Account/classes/PhpFox_ComponentFieldConstructorList.class.php, $Author:  $ */



/** User custom fields list
 *
 * @package module.account
 * @version $Id: PhpFox_ComponentFieldConstructorList.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 *
 */
class PhpFox_Mod_Account_ComponentFieldConstructorList extends BaseComponent
{
    /** Processing method
     */
	function process()
	{
	    $oCustomFieldService = &$this->_oModule->getService('CustomField');
	    /* @var $oCustomFieldService PhpFox_Mod_Account_ServiceCustomField */
	    $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
	    $oReq = &App::getRequest();
	    $oTpl = &$this->_oModule->getTemplate();

	    if (!$oSecurityService->hasStaffPerm('MENU_edit_site_items'))
        {
            App::gotoUrl('admin.main');
        }
        
        if ($oReq->get('duplicates'))
        {
        	$oDb =& Database::get();
        	
        	$aRows = $oDb->getRows("
        		SELECT 
        			custom_field_id, custom_field_name
        		FROM " . App::getT('custom_field') . "        		
        	");
        	
        	$aDelete = array();
        	foreach ($aRows as $aRow) 
        	{
        		$iCnt = $oDb->getField("SELECT COUNT(*) FROM " . App::getT('custom_field') . " WHERE custom_field_name = '" . $oDb->escape($aRow['custom_field_name']) . "'");
        		if ($iCnt > 1)
        		{
        			(!isset($aDelete[$aRow['custom_field_name']]) ? $aDelete[$aRow['custom_field_name']] = 1 : $aDelete[$aRow['custom_field_name']]++);
        			if ($aDelete[$aRow['custom_field_name']] == 1)
        			{
        				continue;        				
        			}
        			// echo "Duplicate: " . $aRow['custom_field_id'] . " -> " . $aRow['custom_field_name'] . "<br />";
        			$oDb->_delete(App::getT('custom_field'), "custom_field_id = '" . $aRow['custom_field_id'] . "'");
        			$oDb->_delete(App::getT('custom_field2text'), "custom_field_id = '" . $aRow['custom_field_id'] . "'");
        			$oDb->_delete(App::getT('custom_field2user'), "custom_field_id = '" . $aRow['custom_field_id'] . "'");
        			$oDb->_delete(App::getT('custom_field_option'), "custom_field_id = '" . $aRow['custom_field_id'] . "'");
        		}
        	}
        	
        	App::gotoUrl('admin.main.custom-fields', array(), App::format('Mod_Account.field.deleted'));
        }
        
	    if ($iFieldId = $oReq->getInt('delete'))
	    {
	        $oCustomFieldService->deleteField($iFieldId);
	        Cache::subStrRemove('custom');
	        App::gotoUrl('admin.main.custom-fields', array(), App::format('Mod_Account.field.deleted'));
	    }
	    if ($oReq->getAction() == 'update')
	    {
	        $aTitles = $oReq->getArray('title');
	        $aPositions = $oReq->getArray('pos');
	        $aSignup = $oReq->getArray('signup');
	        $aPanel = $oReq->getArray('panel');
	        foreach ($aTitles as $iFieldId => $sTitle)
	        {
	            $oCustomFieldService->updateField($iFieldId, $sTitle, $aPositions[$iFieldId], 'default', (isset($aPanel[$iFieldId]) ? $aPanel[$iFieldId] : ''), (isset($aSignup[$iFieldId]) ? $aSignup[$iFieldId] : ''));
	        }
	        Cache::subStrRemove('custom');
	        App::gotoUrl('admin.main.custom-fields', array(), App::format('Mod_Account.field.updated'));
	    }
	    list($aItems, $iCnt) = $oCustomFieldService->getFields(array(), 0, 0, 'custom_field_pos');

	    $oTpl->assign(array(
	        'aItems' => $aItems,
	    ));
	    return $oTpl->fetch('FieldList.html');
	}
}
?>